home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / nyword23.zip / PCWTONYW.ZIP / PCWTONYW.C next >
C/C++ Source or Header  |  1986-12-14  |  6KB  |  220 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /* PCWTONYW.C - changes a PC-WRITE (TM of Quicksoft) PR.DEF file into a  */
  4. /*              PRT file for the New York Word wp.                       */
  5. /*                                                                       */
  6. /* (C) Copyright 1986  Marc Adler    All Rights Reserved                 */
  7. /*************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11.  
  12. char *span_blanks(), *span_chars();
  13. char *strchr();
  14.  
  15. #define ON  1
  16. #define OFF 0
  17.  
  18.  
  19. main(argc, argv)
  20.   int  argc;
  21.   char *argv[];
  22. {
  23.   FILE *inf, *outf, *fopen();
  24.   char *fgets();
  25.   char buf[120], outbuf[80], *s, *t;
  26.   int  letter, state, type, spacing;
  27.  
  28.   /* Open the input file and the output file */
  29.   if (argc < 2)
  30.   {
  31.     printf("Usage input-file [output-file]\n");
  32.     exit(1);
  33.   }
  34.  
  35.   if ((inf  = fopen(argv[1], "r")) == NULL)
  36.   {
  37. cant:
  38.     printf("Can't open files\n");
  39.     exit(1);
  40.   }
  41.  
  42.   strcpy(buf, argv[(argc < 3) ? 1 : 2]);
  43.   if ((s = strchr(buf, '.')) != NULL)
  44.     *s = '\0';
  45.   sprintf(outbuf, "%s.PRT", buf);
  46.   if ((outf = fopen(outbuf, "w")) == NULL)
  47.     goto cant;
  48.  
  49.  
  50.   while (fgets(buf, 120, inf))
  51.   {
  52.     buf[strlen(buf)-1] = '\0';     /* get rid of the trailing newline */
  53.  
  54.     switch (buf[0])                /* examine the first character     */
  55.     {
  56.       case '#'  :
  57.       case '$'  :
  58.                  type   = buf[0];
  59.                  letter = buf[1];
  60.                  state  = ON;
  61.  
  62.                  if (type == '$' &&  (letter == 'S' || letter == 'H'))
  63.                    spacing = atoi(&buf[2]);
  64.  
  65.                  /* = sign is buf[2], number is buf[3] - ..., then blank */
  66.                  if ((s = strchr(buf+1, '=')) == NULL)
  67.                    continue;
  68.                  *s++ = '\0';
  69.  
  70.                  /* For a font char, span the screen attribute chars */
  71.                  if (type == '#' && (s = span_chars(s)) == NULL)
  72.                    continue;
  73.  
  74.                  if ((s = span_blanks(s)) != NULL)
  75.                  {
  76.                    while (*s && *s != ' ')
  77.                    {
  78.                      if (*s == '+')
  79.                      {
  80.                        state = ON;
  81.                        s++;
  82.                      }
  83.                      else if (*s == '-')
  84.                      {
  85.                        state = OFF;
  86.                        s++;
  87.                      }
  88.                      else if (type == '#' || !isdigit(*s))
  89.                        break;
  90.  
  91.                      for (t = outbuf;  isdigit(*s);  )
  92.                      {
  93.                        *t++ = atoi(s);            /* make into a number */
  94.                        while (isdigit(*s))  s++;  /* span the digits    */
  95.                        if (*s == ',')  s++;       /* go past the comma  */
  96.                      }
  97.  
  98.                    output(outf,type,letter,state,outbuf,(int)(t-outbuf),spacing);
  99.  
  100.                    } /* while */
  101.                  }
  102.  
  103.                  break;
  104.  
  105.       case '('  :/* comment line - echo it */
  106.                  fprintf(outf, "# %s\n", buf);
  107.                  break;
  108.  
  109.       default   :
  110.                  break;
  111.  
  112.     }
  113.   }
  114.  
  115.   exit(0);
  116. }
  117.  
  118.  
  119. typedef struct _prtinfo
  120. {
  121.   int  letter;
  122.   char *onname, *offname;
  123. } PRTINFO;
  124.  
  125. PRTINFO font_prtinfo[] =
  126. {
  127.   'B', "bs", "be",     /* bold */
  128.   'C', "cC", "ce",     /* compressed */
  129. /*'D', "ds", "de",*/   /* double width */
  130.   'E', "cE", "ee",     /* elite */
  131. /*'F', "fs", "fe",*/   /* fast */
  132.   'H', "Ss", "Se",     /* superscript */
  133.   'I', "ib", "ie",     /* italics */
  134.   'L', "ss", "se",     /* subscript */
  135. /*'O', "os", "oe",*/   /* overstrike */
  136.   'P', "cP", "pe",     /* pica */
  137.   'Q', "qs", "qe",     /* quality */
  138.   'S', "ds", "de",     /* double strike */
  139.   'U', "us", "ue",     /* underline */
  140.   'V', "ps", "pe",     /* variable */
  141. /*'W', "ws", "we",*/   /* double underline */
  142.    0,   NULL, NULL
  143. };
  144.  
  145. #define FONT_PRTSIZE  (sizeof(font_prtinfo) / sizeof(PRTINFO))
  146.  
  147. PRTINFO ctrl_prtinfo[] =
  148. {
  149.   'P', "is", NULL,     /* initialization sequence */
  150.   'Z', "es", NULL,     /* ending sequence */
  151.   0,   NULL, NULL
  152. };
  153.  
  154. #define CTRL_PRTSIZE  (sizeof(ctrl_prtinfo) / sizeof(PRTINFO))
  155.  
  156.  
  157. output(outf, type, letter, state, outbuf, len, spacing)
  158.   FILE *outf;             /* output file to print to */
  159.   int  type;              /* '#' for font, '$' for control    */
  160.   int  letter, state;     /* starting letter, ON or OFF state */
  161.   char outbuf[];          /* the PC-WRITE sequence string     */
  162.   int  len;               /* length of the PC-WRITE sequence  */
  163.   int  spacing;           /* if $S, the spacing               */
  164. {
  165.   int i, c;
  166.   PRTINFO *p;
  167.  
  168.   /* Check for a bad sequence string */
  169.   if (len <= 0)
  170.     return;
  171.  
  172.   if (type == '$' && letter == 'S')
  173.     fprintf(outf, "L%d=", spacing);
  174.   else if (type == '$' && letter == 'H')
  175.     fprintf(outf, "C%d=", spacing);
  176.   else
  177.   {
  178.     /* Search the Prtinfo table for the matching letter */
  179.     p = (type == '#') ? font_prtinfo : ctrl_prtinfo;
  180.     for (  ;  p->letter && letter != p->letter;  p++) ;
  181.  
  182.     /* Didn't find a match */
  183.     if (p->letter == 0)  return;
  184.  
  185.     /* Print the proper NYWord two-letter attribute name, followed by '=' */
  186.     fprintf(outf, "%s=", (state == ON) ? p->onname : p->offname);
  187.   }
  188.  
  189.   /* Translate the PC-WRITE codes into the NYWord sequence string */
  190.   for (i = 0;  i < len;  i++)
  191.     if ((c = outbuf[i]) == 0)         /* special zero byte is \Z */
  192.       fprintf(outf, "\\Z");
  193.     else if (c == 27)                 /* ESC is \E */
  194.       fprintf(outf, "\\E");
  195.     else if (c < ' ' || c > 126)      /* control char is 3 digit number */
  196.     {
  197.       putc('\\', outf);
  198.       fprintf(outf, "%03d", c);
  199.     }
  200.     else                              /* regular character */
  201.       fprintf(outf, "%c", c);
  202.  
  203.   putc('\n', outf);
  204. }
  205.  
  206.  
  207. char *span_blanks(s)
  208.   char *s;
  209. {
  210.   while (*s && isspace(*s))  s++;
  211.   return (*s == '\0') ? NULL : s;
  212. }
  213.  
  214. char *span_chars(s)
  215.   char *s;
  216. {
  217.   while (*s && !isspace(*s))  s++;
  218.   return (*s == '\0') ? NULL : s;
  219. }
  220.